import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)



const router = new VueRouter({
  routes: [
    { path: '/login', component: () => import('@/components/MyLogin.vue') },
    {
      path: '/', component: () => import('@/components/MyHome.vue'), children: [
        { path: 'users', component: () => import('@/components/menus/MyUsers.vue') },
        { path: 'orders', component: () => import('@/components/menus/MyOrders.vue') },
        { path: 'rights', component: () => import('@/components/menus/MyRights.vue') },
        { path: 'settings', component: () => import('@/components/menus/MySettings.vue') },
        { path: 'goods', component: () => import('@/components/menus/MyGoods.vue') },
        { path: 'det/:id', component: () => import('@/components/user/MyUserDetail.vue') }
      ]
    }
  ]
})

// 白名单 ：无需有没有token都可以访问到的
const Whitelist = ['/login', '404']

const token = localStorage.getItem('token')

router.beforeEach((to, from, next) => {

  console.log(to.path)

  // 有 token 的情况下
  if (token) {

    // 有 token 说明已经登陆过了 你再想登录 不让你登
    if (to.paht === '/login') {
      // 跳转到首页
      next('/')

    } else {
      // 去别的地方放行
      next()
    }

    // 没有token
  } else {

    // 如果白名单有这个路径放行
    if ((Whitelist.includes(to.path))) {

      next()

      // 没有去登录页
    } else {
      next('/login')
    }


  }

})

export default router
